package com.wxg.audio;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;


/**
 * 
  Unable to handle format: mpeglayer3, 16000.0 Hz, 16-bit, Mono, LittleEndian, Signed, 2000.0 frame rate, FrameSize=16384 bits
Failed to realize: com.sun.media.PlaybackEngine@3abfe836
Error: Unable to realize com.sun.media.PlaybackEngine@3abfe836
 * 
 * 
 * @author pactera
 * 
 * `com.wxg.audio.PlayerMusic`不能正确播放,请参考`com.wxg.audio.MP3Player`
 * `com.wxg.audio.PlayerMusic`需要"jmf.jar"
 * `com.wxg.audio.MP3Player`需要"jl1.0.1.jar,tritonus_share.jar,mp3spi1.9.5.jar"
 * 
 * 
 */
@Deprecated
public class PlayerMusic implements ControllerListener {
	// 播放对象
	private Player player;
	// 是否循环播放
	@SuppressWarnings("unused")
	private boolean first, loop;
	// 文件路径
	@SuppressWarnings("unused")
	private String path;
	// 存放MP3文件
	private List<String> mp3List;
	// 当前MP3文件数量
	private int mp3NO = 0;

	PlayerMusic(List<String> mp3List) {
		this.mp3List = mp3List;
	}

	// 播放方法
	public void start() {
		try {
			player = Manager.createPlayer(new File(mp3List.get(mp3NO)).toURI().toURL());
		} catch (NoPlayerException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
			System.out.println("不能播放此文件！");
			return;
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
			return;
		}
		if (player == null) {
			System.out.println("播放文件为空！");
			return;
		}
		player.addControllerListener(this);
		// 提取媒体内容
		player.prefetch();
	}

	public void controllerUpdate(ControllerEvent e) {
		// 当媒体播放结束时，循环播放
		if (e instanceof EndOfMediaEvent) {
			mp3NO++;
			System.out.println(mp3NO);
			if (mp3NO < mp3List.size()) {

				this.start();
			}
			return;
		}

		// 当提取媒体的内容结束
		if (e instanceof PrefetchCompleteEvent) {
			System.out.println("内容结束");
			player.start();
			return;
		}

		// 当实例化后
		if (e instanceof RealizeCompleteEvent) {
			System.out.println("实例化");
			// pack(); //执行pack()操作
			return;
		}

	}

	public static void main(String[] args) {
		List<String> path = new ArrayList<String>();
//		path.add("X:\\Song\\只对你有感觉.mp3");
		path.add("C:\\Users\\pactera\\Desktop\\444\\world_en.mp3");
		PlayerMusic play = new PlayerMusic(path);
		play.start();
	}
}